package com.hd.assistant.biz.manager.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.hd.assistant.biz.manager.AssistantManager;
import com.hd.assistant.biz.manager.exception.ManagerException;
import com.hd.assistant.dal.dao.ClickedDetailDAO;
import com.hd.assistant.dal.dao.UserDao;
import com.hd.assistant.dal.dao.exception.DAOException;
import com.hd.assistant.dal.dao.query.ClickedDetailQuery;
import com.hd.assistant.dal.dataobject.ClickedDetail;
import com.hd.assistant.dal.dataobject.User;

public class AssistantManagerImpl implements AssistantManager{
	private final static Logger log = LoggerFactory.getLogger(AssistantManagerImpl.class);
	final String ERROE_PRE = "发生错误 请联系客服！ AssistantManagerImpl:";
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private ClickedDetailDAO clickedDetailDAO;
	public User getUserByUserIdAndType(Long userId, String userType)
			throws ManagerException {
		try {
			return userDao.getUserByUserIdAndType(userId, userType);
		} catch (DAOException e) {
			String error = ERROE_PRE + "getUserByUserIdAndType";
			log.error(error,e);
			throw new ManagerException(error,e);
		}
	}

	public void insertUser(User user) throws ManagerException {
		try {
			userDao.insertUser(user);
		} catch (DAOException e) {
			String error = ERROE_PRE + "insertUser";
			log.error(error,e);
			throw new ManagerException(error,e);
		}
	}

	public void updateUser(User user) throws ManagerException {
		try {
			userDao.updateUser(user);
		} catch (DAOException e) {
			String error = ERROE_PRE + "updateUser";
			log.error(error,e);
			throw new ManagerException(error,e);
		}
	}

	public void insertClickedDetail(ClickedDetail item) throws ManagerException {
		try {
			clickedDetailDAO.insertClickedDetail(item);
		} catch (DAOException e) {
			String error = ERROE_PRE + "insertClickedDetail";
			log.error(error,e);
			throw new ManagerException(error,e);
		}
	}

	public ClickedDetail getNearestClickedDetail(String clickedId) throws ManagerException {
		try {
			return clickedDetailDAO.getNearestByClickedId(clickedId);
		} catch (DAOException e) {
			String error = ERROE_PRE + "getNearestClickedDetail";
			log.error(error,e);
			throw new ManagerException(error,e);
		}
	}

	public List<ClickedDetail> getAllDetailsByItemId(String itemId)
			throws ManagerException {
		if(itemId == null)
			return null;
		ClickedDetailQuery query = new ClickedDetailQuery();
		query.setItemId(itemId);
		try {
			return clickedDetailDAO.getAllDetailsByQuery(query);
		} catch (DAOException e) {
			String error = ERROE_PRE + "getAllDetailsByQuery";
			log.error(error,e);
			throw new ManagerException(error,e);
		}
	}

	public List<ClickedDetail> getAllDetailsSumByUserId(String userId,
			String beginDate, String endDate) throws ManagerException {
		if(userId == null)
			return null;
		ClickedDetailQuery query = new ClickedDetailQuery();
		query.setUserId(userId);
		if(beginDate != null)
			query.setClickedDateFrom(beginDate);
		
		if(endDate != null)
			query.setClickedDateTo(endDate);
		try {
			return clickedDetailDAO.getAllDetailsGroupByByItemidAndUserId(query);
		} catch (DAOException e) {
			String error = ERROE_PRE + "getAllDetailsSumByUserId";
			log.error(error,e);
			throw new ManagerException(error,e);
		}
	}

	public List<ClickedDetail> getAllDetailsDailySumByUserId(String userId)
			throws ManagerException {
		if(userId == null)
			return null;
		ClickedDetailQuery query = new ClickedDetailQuery();
		query.setUserId(userId);
		try {
			return clickedDetailDAO.getAllDetailsGroupByByClickedDateAndUserId(query);
		} catch (DAOException e) {
			String error = ERROE_PRE + "getAllDetailsDailySumByUserId";
			log.error(error,e);
			throw new ManagerException(error,e);
		}
	}
}
